<template>
  <el-dialog
      :title="!dataForm.ysId ? '新增' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
<el-form-item label="医生姓名" prop="ysName">
  <el-input v-model="dataForm.ysName" placeholder="医生姓名"></el-input>

</el-form-item>
<el-form-item label="医生性别" prop="ysSex">
  <el-select placeholder="医生性别" v-model="dataForm.ysSex">
    <el-option label="男" :value="1"></el-option>
    <el-option label="女" :value="0"></el-option>
  </el-select>
</el-form-item>
<el-form-item label="医生科室" prop="ysKs">
  <el-input v-model="dataForm.ysKs" placeholder="医生科室"></el-input>

</el-form-item>
<el-form-item label="联系电话" prop="ysPhone">
  <el-input v-model="dataForm.ysPhone" placeholder="联系电话"></el-input>

</el-form-item>
<el-form-item label="登录密码" prop="ysPassword">
  <el-input v-model="dataForm.ysPassword" placeholder="登录密码"></el-input>

</el-form-item>
<el-form-item label="出生日期" prop="ysBirthDate">
  <el-date-picker v-model="dataForm.ysBirthDate"
                  placeholder="出生日期"></el-date-picker>
</el-form-item>
<el-form-item label="创建时间" prop="ysCreateDate">
  <el-date-picker v-model="dataForm.ysCreateDate"
                  placeholder="创建时间"></el-date-picker>
</el-form-item>
<el-form-item label="医生头像" prop="ysImg">
  <el-input v-model="dataForm.ysImg" placeholder="医生头像"></el-input>

</el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          ysId: 0,
          ysName: '',
          ysSex: '',
          ysKs: '',
          ysPhone: '',
          ysPassword: '',
          ysBirthDate: '',
          ysCreateDate: '',
          ysImg: ''
        },
        dataRule: {
          ysName: [
            { required: true, message: '医生姓名不能为空', trigger: 'blur' }
          ],
          ysSex: [
            { required: true, message: '医生性别不能为空', trigger: 'blur' }
          ],
          ysKs: [
            { required: true, message: '医生科室不能为空', trigger: 'blur' }
          ],
          ysPhone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          ysPassword: [
            { required: true, message: '登录密码不能为空', trigger: 'blur' }
          ],
          ysBirthDate: [
            { required: true, message: '出生日期不能为空', trigger: 'blur' }
          ],
          ysCreateDate: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          ysImg: [
            { required: true, message: '医生头像不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.ysId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.ysId) {
            this.$http({
              url: this.$http.adornUrl(`/hospital/ys/info/${this.dataForm.ysId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.ysName = data.ys.ysName
                this.dataForm.ysSex = data.ys.ysSex
                this.dataForm.ysKs = data.ys.ysKs
                this.dataForm.ysPhone = data.ys.ysPhone
                this.dataForm.ysPassword = data.ys.ysPassword
                this.dataForm.ysBirthDate = data.ys.ysBirthDate
                this.dataForm.ysCreateDate = data.ys.ysCreateDate
                this.dataForm.ysImg = data.ys.ysImg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hospital/ys/${!this.dataForm.ysId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'ysId': this.dataForm.ysId || undefined,
                'ysName': this.dataForm.ysName,
                'ysSex': this.dataForm.ysSex,
                'ysKs': this.dataForm.ysKs,
                'ysPhone': this.dataForm.ysPhone,
                'ysPassword': this.dataForm.ysPassword,
                'ysBirthDate': this.dataForm.ysBirthDate,
                'ysCreateDate': this.dataForm.ysCreateDate,
                'ysImg': this.dataForm.ysImg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
